package org.fas.net
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import org.fas.event.FNetEvent;

	public class FUrl extends URLLoader
	{
		public var lastUrl:String;
		private var result:Function;
		public function FUrl()
		{
			super();
		}
		public function loadIn(_url:String,_result:Function,_params:Object=null):void{
			this.result = _result;
			lastUrl = _url;
			var loader:URLLoader = new URLLoader();
			var req:URLRequest = new URLRequest(_url);
			if(_params==null){
				req.method = URLRequestMethod.GET;	
			}else{
				req.method = URLRequestMethod.POST;
				req.data = new URLVariables();
	            var _param:String;
	            for(_param in _params){
	            	req.data[_param] = _params[_param];
	            }
			}
        	loader.addEventListener(Event.COMPLETE,completeHandle);
        	loader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
        	loader.load(req);
		}
		public function completeHandle(_e:Event):void{
			_e.target.removeEventListener(Event.COMPLETE,completeHandle);
        	_e.target.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
        	var __e:FNetEvent = new FNetEvent(Event.COMPLETE);
        	__e.data = _e.target.data;
			this.dispatchEvent(__e);
			this.result(_e.target.data);
		}
		public function ioErrorHandler(_e:IOErrorEvent):void{
//			trace("io error");
			throw(new Error("FUrl.io error,url:"+this.lastUrl));
		}
	}
}